from __main__ import game;
from toee import *
from utilities import *

def san_dialog( attachee, triggerer ):
	game.global_flags[834] = 1 
	if (anyone( triggerer.group_list(), "has_item", 11003 ) and attachee.item_find(11003) == OBJ_HANDLE_NULL):
		if ( game.global_flags[198] == 1 ):
			triggerer.begin_dialog( attachee, 1000 )
		elif (attachee.leader_get() != OBJ_HANDLE_NULL):
			if (game.global_flags[53] == 1):
				triggerer.begin_dialog( attachee, 1100 )
			else:
				triggerer.begin_dialog( attachee, 1200 )
		elif ( game.global_flags[52] == 1 ):
			triggerer.begin_dialog( attachee, 1300 )
		elif ( game.global_flags[48] == 1 ):
			triggerer.begin_dialog( attachee, 1400 )
		else:
			triggerer.begin_dialog( attachee, 1500 )
	elif ( game.global_flags[198] == 1 ):
		triggerer.begin_dialog( attachee, 260 )
	elif (attachee.leader_get() != OBJ_HANDLE_NULL):
		if (game.global_flags[53] == 1):
			triggerer.begin_dialog( attachee, 320 )
		else:
			triggerer.begin_dialog( attachee, 220 )
	elif ( game.global_flags[52] == 1 ):
		triggerer.begin_dialog( attachee, 20 )
	elif ( game.global_flags[48] == 1 ):
		triggerer.begin_dialog( attachee, 1 )
	else:
		triggerer.begin_dialog( attachee, 10 )
	return SKIP_DEFAULT


def san_first_heartbeat( attachee, triggerer ):
	if (attachee.leader_get() == OBJ_HANDLE_NULL and not attachee.has_spell_effects() and (game.party_alignment == LAWFUL_NEUTRAL or game.party_alignment == LAWFUL_EVIL or game.party_alignment == LAWFUL_GOOD or game.party_alignment == TRUE_NEUTRAL)):
		attachee.cast_spell(spell_detect_law, attachee)
		attachee.spells_pending_to_memorized()
		game.global_vars[726] = 2
	if (attachee.leader_get() == OBJ_HANDLE_NULL and not attachee.has_spell_effects() and (game.party_alignment == CHAOTIC_NEUTRAL or game.party_alignment == CHAOTIC_GOOD or game.party_alignment == CHAOTIC_EVIL)):
		attachee.cast_spell(spell_detect_chaos, attachee)
		attachee.spells_pending_to_memorized()
		game.global_vars[726] = 2
	if (game.party_alignment == NEUTRAL_GOOD and attachee.leader_get() == OBJ_HANDLE_NULL and not attachee.has_spell_effects()):
		attachee.cast_spell(spell_detect_good, attachee)
		attachee.spells_pending_to_memorized()
		game.global_vars[726] = 2
	if (game.party_alignment == NEUTRAL_EVIL and attachee.leader_get() == OBJ_HANDLE_NULL and not attachee.has_spell_effects()):
		attachee.cast_spell(spell_detect_evil, attachee)
		attachee.spells_pending_to_memorized()
		game.global_vars[726] = 2
	return RUN_DEFAULT

def san_heartbeat( attachee, triggerer ):
	game.global_vars[726] = game.global_vars[726] + 1
	if (game.combat_is_active()):
		return RUN_DEFAULT
	if (attachee.leader_get() == OBJ_HANDLE_NULL and (not attachee.has_wielded(1) or not attachee.has_wielded(4071))):
		attachee.item_wield_best_all()
		attachee.item_wield_best_all()
	if (game.global_vars[726] == 1 and attachee.leader_get() == OBJ_HANDLE_NULL and (game.party_alignment == LAWFUL_NEUTRAL or game.party_alignment == LAWFUL_EVIL or game.party_alignment == LAWFUL_GOOD or game.party_alignment == TRUE_NEUTRAL)):
		attachee.cast_spell(spell_detect_law, attachee)
		attachee.spells_pending_to_memorized()
	if (game.global_vars[726] == 1 and attachee.leader_get() == OBJ_HANDLE_NULL and (game.party_alignment == CHAOTIC_NEUTRAL or game.party_alignment == CHAOTIC_GOOD or game.party_alignment == CHAOTIC_EVIL)):
		attachee.cast_spell(spell_detect_chaos, attachee)
		attachee.spells_pending_to_memorized()
	if (game.global_vars[726] == 1 and game.party_alignment == NEUTRAL_GOOD and attachee.leader_get() == OBJ_HANDLE_NULL):
		attachee.cast_spell(spell_detect_good, attachee)
		attachee.spells_pending_to_memorized()
	if (game.global_vars[726] == 1 and game.party_alignment == NEUTRAL_EVIL and attachee.leader_get() == OBJ_HANDLE_NULL):
		attachee.cast_spell(spell_detect_evil, attachee)
		attachee.spells_pending_to_memorized()
	for obj in game.party[0].group_list():
		if (attachee.distance_to(obj) <= 10 and game.global_vars[726] == 5):
			if (attachee.leader_get() == OBJ_HANDLE_NULL and (game.party_alignment == LAWFUL_NEUTRAL or game.party_alignment == LAWFUL_EVIL or game.party_alignment == LAWFUL_GOOD or game.party_alignment == TRUE_NEUTRAL)):
				attachee.cast_spell(spell_magic_circle_against_law, attachee)
				attachee.spells_pending_to_memorized()
#				return RUN_DEFAULT
	for obj in game.party[0].group_list():
		if (attachee.distance_to(obj) <= 10 and game.global_vars[726] == 5):
			if (attachee.leader_get() == OBJ_HANDLE_NULL and (game.party_alignment == CHAOTIC_NEUTRAL or game.party_alignment == CHAOTIC_GOOD or game.party_alignment == CHAOTIC_EVIL)):
				attachee.cast_spell(spell_magic_circle_against_chaos, attachee)
				attachee.spells_pending_to_memorized()
#				return RUN_DEFAULT
	for obj in game.party[0].group_list():
		if (attachee.distance_to(obj) <= 10 and game.global_vars[726] == 5):
			if (game.party_alignment == NEUTRAL_GOOD and attachee.leader_get() == OBJ_HANDLE_NULL):
				attachee.cast_spell(spell_magic_circle_against_good, attachee)
				attachee.spells_pending_to_memorized()
#				return RUN_DEFAULT
	for obj in game.party[0].group_list():
		if (attachee.distance_to(obj) <= 10 and game.global_vars[726] == 5):
			if (game.party_alignment == NEUTRAL_EVIL and attachee.leader_get() == OBJ_HANDLE_NULL):
				attachee.cast_spell(spell_magic_circle_against_evil, attachee)
				attachee.spells_pending_to_memorized()
#				return RUN_DEFAULT	
	if (game.global_vars[726] >= 200):
		game.global_vars[726] = 0
	return RUN_DEFAULT


def san_dying( attachee, triggerer ):
	if (attachee.leader_get() != OBJ_HANDLE_NULL):
		game.global_vars[29] = game.global_vars[29] + 1
		game.global_flags[37] = 1
		if ( game.story_state <= 1 ):
			game.story_state = 2
		return RUN_DEFAULT
	attachee.float_line(12014,triggerer)
	game.global_flags[37] = 1
	if ( game.story_state <= 1 ):
		game.story_state = 2
	for pc in game.party:
		if ( pc.reputation_has( 18 ) == 1):
			pc.reputation_remove( 18 )
	attachee.float_line(12014,triggerer)
	game.party[0].reputation_add( 15 )
	if ( game.global_flags[340] == 1 ):
		for obj in game.obj_list_vicinity(attachee.location,OLC_PC):
			if (is_safe_to_talk(attachee,obj)):
				game.global_flags[834] = 1 
				obj.begin_dialog( attachee, 370 )
				attachee.float_line(12014,triggerer)
	elif ( game.global_flags[62] == 0 ):
		for obj in game.obj_list_vicinity(attachee.location,OLC_PC):
			if (is_safe_to_talk(attachee,obj)):
				game.global_flags[834] = 1 
				obj.begin_dialog( attachee, 390 )
				attachee.float_line(12014,triggerer)
	return RUN_DEFAULT

def	san_disband( attachee, triggerer ):
	if (game.global_flags[806] == 0):
		for pc in game.party:
			attachee.ai_shitlist_remove( pc )
			attachee.reaction_set( pc, 50 )
		for npc in game.obj_list_vicinity(attachee.location,OLC_NPC):
			for pc in game.party:
				npc.ai_shitlist_remove( pc )
				npc.reaction_set( pc, 50 )
	return RUN_DEFAULT

def san_join( attachee, triggerer ):
	if ( game.story_state <= 1 and game.global_flags[806] == 0):
		game.story_state = 2	
	if ( game.global_flags[340] == 1 and game.global_flags[806] == 0):
		game.global_flags[834] = 1 
		triggerer.begin_dialog( attachee, 380 )
	return RUN_DEFAULT

def san_resurrect( attachee, triggerer ):
	game.global_flags[37] = 0
	for pc in game.party:
		if ( pc.reputation_has( 15 ) == 1):
			pc.reputation_remove( 15 )
	for pc in game.party:
		if ( pc.reputation_has( 18 ) == 0):
			pc.reputation_add( 18 )
	return RUN_DEFAULT

def san_enter_combat( attachee, triggerer ):
	attachee.float_line(12058,triggerer)
	if (attachee.leader_get() == OBJ_HANDLE_NULL and (not attachee.has_wielded(1) or not attachee.has_wielded(4071))):
		attachee.item_wield_best_all()
		game.new_sid = 0
	return RUN_DEFAULT

def san_start_combat( attachee, triggerer ):
	if ( obj_percent_hp(attachee) < 50):
		found_pc = OBJ_HANDLE_NULL
		for pc in game.party:
			if pc.type == obj_t_pc:
				found_pc = pc
				attachee.ai_shitlist_remove( pc )
		if found_pc != OBJ_HANDLE_NULL:
			game.global_flags[834] = 1 
			found_pc.begin_dialog( attachee, 160 )
			game.new_sid = 0
			return SKIP_DEFAULT
	return RUN_DEFAULT

def san_new_map( attachee, triggerer ):
	randy1 = game.random_range(1,12)		# added by ShiningTed
#	if (attachee.map == 5065):				## Removed by Livonya
#		leader = attachee.leader_get()		## Removed by Livonya
#		if (leader != OBJ_HANDLE_NULL):		## Removed by Livonya
#			leader.begin_dialog( attachee, 270 )## Removed by Livonya
	if ((attachee.map == 5113) and ( game.global_flags[200] == 0 )):
		game.global_flags[200] = 1
		leader = attachee.leader_get()
		if (leader != OBJ_HANDLE_NULL):
			leader.begin_dialog( attachee, 290 )
	if (attachee.map == 5066):
		leader = attachee.leader_get()
		if (leader != OBJ_HANDLE_NULL):
			leader.begin_dialog( attachee, 300 )
	if (game.global_flags[833] == 0 and (attachee.map == 5001 or attachee.map == 5051)):
		leader = attachee.leader_get()
		if (leader != OBJ_HANDLE_NULL):
			leader.begin_dialog( attachee, 600 )
	if ((attachee.map == 5013) and randy1 >= 6):	# added by ShiningTed
		attachee.float_line(12029,triggerer)
	if ((attachee.map == 5042) and randy1 >= 8):	# added by ShiningTed
		attachee.float_line(12060,triggerer)
	return RUN_DEFAULT

def buff_npc( attachee, triggerer ):
	for obj in game.obj_list_vicinity(attachee.location,OLC_NPC):
		if (obj.name == 14424 and obj.leader_get() == OBJ_HANDLE_NULL):
			obj.turn_towards(attachee)
			obj.cast_spell(spell_mage_armor, obj)
		if (obj.name == 14425 and obj.leader_get() == OBJ_HANDLE_NULL):
			obj.turn_towards(attachee)
			obj.cast_spell(spell_shield_of_faith, obj)
	attachee.cast_spell(spell_shield_of_faith, attachee)
	return RUN_DEFAULT

def buff_npc_two( attachee, triggerer ):
	for obj in game.obj_list_vicinity(attachee.location,OLC_NPC):
		if (obj.name == 14424 and obj.leader_get() == OBJ_HANDLE_NULL):
			obj.cast_spell(spell_mirror_image, obj)
		if (obj.name == 14425 and obj.leader_get() == OBJ_HANDLE_NULL):
			obj.cast_spell(spell_owls_wisdom, obj)
	attachee.cast_spell(spell_bulls_strength, attachee)
	return RUN_DEFAULT

def buff_npc_three( attachee, triggerer ):
	if (not attachee.has_wielded(1) or not attachee.has_wielded(4071)):
		attachee.item_wield_best_all()
	return RUN_DEFAULT

def run_off( attachee, triggerer ):
	for pc in game.party:
		attachee.ai_shitlist_remove( pc )
		attachee.reaction_set( pc, 50 )
	attachee.runoff(attachee.location-3)
	return RUN_DEFAULT

def	demo_end_game( attachee, triggerer ):
	# play slides and end game
	return RUN_DEFAULT

def argue_ron( attachee, triggerer, line): # added by ShiningTed
	npc = find_npc_near(attachee,14681)
	if (npc != OBJ_HANDLE_NULL):
		triggerer.begin_dialog(npc,line)
		npc.turn_towards(triggerer)
		triggerer.turn_towards(npc)
	else:
		triggerer.begin_dialog(attachee,260)
	return SKIP_DEFAULT

def equip_transfer( attachee, triggerer ):
	itemA = attachee.item_find(4071)
	if (itemA != OBJ_HANDLE_NULL):
		itemA.destroy()
		create_item_in_inventory( 4071, triggerer )
	create_item_in_inventory( 7001, attachee )
	return RUN_DEFAULT

def create_spiders( attachee, triggerer ): # added by ShiningTed
	spider1 = game.obj_create( 14397, location_from_axis (474L, 535L) )
	game.particles( "sp-summon monster I", spider1 )
	game.sound( 4033, 1 )
	spider1.turn_towards(game.party[0])
	spider2 = game.obj_create( 14398, location_from_axis (481L, 536L) )
	game.particles( "sp-summon monster I", spider2 )
	spider2.turn_towards(game.party[0])
	spider3 = game.obj_create( 14620, location_from_axis (470L, 536L) )
	game.particles( "sp-summon monster I", spider3 )
	spider3.turn_towards(game.party[0])
	spider4 = game.obj_create( 14417, location_from_axis (529L, 544L) )
	spider5 = game.obj_create( 14417, location_from_axis (532L, 555L) )
	return RUN_DEFAULT
